import { createRouter, createWebHashHistory } from 'vue-router'
const routes = [
  // 路由配置
  {
    path: '/',
    name: 'Index',
    redirect: '/home'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue')
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('@/views/home/index.vue'),
    redirect: '/version',
    children: [
      {
        path: '/version',
        name: 'Version',
        component: () => import('@/components/Versions.vue')
      },
      {
        path: '/open_file',
        name: 'OpenFile',
        component: () => import('@/views/openFiles/index.vue')
      }
    ]
  }
]
const router = createRouter({
  mode: 'hash', // 路由模式
  history: createWebHashHistory(),
  routes
})
router.beforeEach((to, from, next) => {
  // 判断是否需要登录权限
  const token = localStorage.getItem('ACCESS_TOKEN')
  console.log(token, 'token')
  console.log(from, 'from.path')
  console.log(to.path, 'to.path')
  if (token) {
    // window.api.apiIsMainWindow()
    if (to.path === '/login') {
      next('/home')
    } else {
      next()
    }
    // next()
  } else {
    if (to.path === '/login') {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
